package no.rodland.acc.chart;

import org.joda.time.DateTime;
import org.joda.time.Days;

import java.text.DecimalFormat;

import no.rodland.acc.db.DbRow;

import static java.lang.Math.random;

/**
 *
 */
public class Element {
    private String type = "line";
    private Dot values[];
    private String colour;

    public Element(int numberOfDataPoints) {
        this.values = new Dot[numberOfDataPoints];
        this.colour = "#" + randomHex() + randomHex() + randomHex();
    }

    private String generateTip(DbRow row) {
        StringBuilder builder = new StringBuilder();
        builder.append(row);
        return builder.toString();
    }

    private String format(double value, String format) {
        DecimalFormat formatter = new DecimalFormat(format);
        return formatter.format(value);
    }

    private String randomHex() {
        return Integer.toHexString((int) (random() * 255d));
    }

    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Dot[] getValues() {
        return values;
    }

    public void setValues(Dot[] values) {
        this.values = values;
    }

    public void addData(DateTime date, Double revenue, DbRow row) {
        DateTime today = new DateTime().minusDays(1);
        int index = values.length - 1 - Days.daysBetween(date, today).getDays();
        values[index] = new Dot(generateTip(row), revenue);
    }
}
